<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Labor;
use App\Models\LaborTeam;
use App\Models\Project;
use App\Services\ProjectService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ProjectController extends Controller
{
    protected $ProjectService;
    public function __construct(ProjectService $ProjectService)
    {
        $this->ProjectService = $ProjectService;
    }

    public function show(Request $request)
    {

        $data = Project::select('id','name','number','item_name','item_number','start_date','end_date','attendance_type')->where('number',$request->number)->first();
        if(!$data){
            return response()->json(['code' => 404, 'message' => '请重新扫码']);
        }
        if($data->start_date){
            $data->start_date = strtotime($data->start_date) * 1000;
        }
        if($data->end_date){
            $data->end_date = strtotime($data->end_date) * 1000;
        }
        $data->teams;
        $teamids = [];
        $teams = [];
        foreach ($data->teams as $k => $v) {
            $arr = [];
            if($v->team){
                $teamids[] = $v->id;
                $arr['id'] = $v->team_id;
                $arr['name'] = $v->team['name'];
                $arr['director'] = $v->team['director'];
                $arr['contact_mobile'] = $v->team['contact_mobile'];
                $arr['type'] = $v->team['type'];
                $teams[] = $arr;
            }
        }
        $data->teamids = $teamids;
        $labor = null;
        if (Auth::guard('api')->check()) {
            $user = Auth::guard('api')->user();
            // select('id','name','mobile','id_card','bank_number','line_number','bank','bank_deposit','emergency_contact','emergency_mobile','photo','id_card_personal','id_card_emblem')->
            $labor = Labor::where('id',$user->id)->first();
        }
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data,'labor'=>$labor,'teams'=>$teams]);
    }

    public function projecTteam(Request $request)
    {
        $data = Project::select('id','name','number','item_name','item_number','start_date','end_date')->where('number',$request->number)->first();
        if($data->start_date){
            $data->start_date = strtotime($data->start_date) * 1000;
        }
        if($data->end_date){
            $data->end_date = strtotime($data->end_date) * 1000;
        }
        $data->teams;
        $teamids = [];
        $teams = [];
        foreach ($data->teams as $k => $v) {
            $arr = [];
            if($v->team){
                $teamids[] = $v->id;
                $arr['id'] = $v->team_id;
                $arr['name'] = $v->team['name'];
                $arr['director'] = $v->team['director'];
                $arr['contact_mobile'] = $v->team['contact_mobile'];
                $arr['type'] = $v->team['type'];
                $teams[] = $arr;
            }
        }
        $data->teamids = $teamids;
        $data->team = '';
        foreach ($teams as $k => $v) {
            if($request->team_id){
                $data->team = $v;
            }
        }

        //判断当前登录的人是谁
        $data->is_leader = false;
        if($request->team_id){
            $user = Auth::user();
            $laborTeam = LaborTeam::find($request->team_id);
            if($laborTeam->scene_mobile == $user->mobile){
                $data->is_leader = true;
            }

            if($laborTeam->contact_mobile == $user->mobile){
                $data->is_leader = true;
            }
        }

        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data,'teams'=>$teams]);
    }


}
